﻿Imports System.Data.OleDb
Public Class editprofile
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
            Dim SqlString As String = "select * from profile where cookname = @f1"
            Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
            SqlString = "select * from profile where cookname = @f1"
            oleDbConn.Open()
            ' to get records back from a database
            Dim dr As OleDbDataReader = cmd.ExecuteReader()
            If dr.HasRows Then
                dr.Read()
                If Not IsDBNull(dr("firstname")) Then txtfirstname.Text = dr("firstname")
                If Not IsDBNull(dr("lastname")) Then txtlastname.Text = dr("lastname")
                If Not IsDBNull(dr("description")) Then txtdescription.Text = dr("description")
            End If
        End If
    End Sub

    Protected Sub cmdsaveprofile_Click(ByVal sender As Object, ByVal e As EventArgs) Handles cmdsaveprofile.Click
        Dim newfilename As String = ""
        Dim SqlString As String
        If fileuploadpics.HasFile Then
            Dim myguid = Guid.NewGuid()
            newfilename = myguid.ToString() & ".jpg"
            Dim piclocationonserverhardisk = Request.MapPath("pictures") & "/" & fileuploadpics.FileName
            fileuploadpics.SaveAs(piclocationonserverhardisk)
        End If
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        If fileuploadpics.HasFile Then
            SqlString = "UPDATE profile SET firstname=@f1, lastname=@f2, description=@f3, picture=@f4 where cookname=@f5"
        Else
            SqlString = "UPDATE profile SET firstname=@f1, lastname=@f2, description=@f3 where cookname=@f5"
        End If

        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", txtfirstname.Text)
        cmd.Parameters.AddWithValue("@f2", txtlastname.Text)
        cmd.Parameters.AddWithValue("@f3", txtdescription.Text)
        If fileuploadpics.HasFile Then cmd.Parameters.AddWithValue("@f4", "pictures/" & newfilename)
        cmd.Parameters.AddWithValue("@f5", User.Identity.Name)
        oleDbConn.Open()
        cmd.ExecuteNonQuery()
        '   cmd.Dispose()
        '  oleDbConn.Close()
        '   oleDbConn.Dispose()
        Response.Redirect("profile.aspx")
    End Sub


End Class